﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeneralLibs;
using System.Data.SqlClient;

namespace BasicForms
{
    public partial class FrmExceptionNotifier : BasicForms.FrmBase
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            try
            {
                switch (keyData)
                {
                    case Keys.Enter:
                        this.Close();
                        break;
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private FrmExceptionNotifier(string exceptionDescription, string exceptionPosition)
        {
            InitializeComponent();
            this.AcceptButton = btnClose;
            txtExceptionDescription.Text = exceptionDescription;
            txtExceptionPosition.Text = exceptionPosition;
            GeneralLibs.CustomException.WriteLog(string.Format("Lỗi \"{0}\" tại {1}\n-----------------------------------------------------\n", 
                exceptionDescription, exceptionPosition));
        }

        public static void ShowAndLog(Exception ex)
        {
            BasicForms.FrmProgressBarDev.FinishShowProgress();
            if (ex is CustomException)
            {
                switch((ex as CustomException).ExceptionType)
                {
                    case ExceptionType.XuatHangAm:
                        FrmWarning.Show("Cảnh báo xuất âm", ex.Message);
                        break;
                    case ExceptionType.LackOfInputInfo:
                    case ExceptionType.InvalidInputInfo:
                        FrmWarning.Show("Thông báo", ex.Message);
                        break;
                    default:
                        FrmWarning.Show("Thông báo", ex.Message);
                        break;
                }
            }
            else if (ex is SqlException)
            {
                FrmExceptionNotifier frm = new FrmExceptionNotifier( CustomException.GetSqlExceptionMessage(ex as SqlException), 
                                        ex.StackTrace.Substring(3));
                frm.ShowDialog();
                frm.Focus();
                frm.btnClose.Focus();
            }
            else if (ex.Message == "The data area passed to a system call is too small)")
            {
                FrmWarning.Show("Thông báo", "Dữ liệu vừa nhập có giá trị nhỏ hơn cho phép");
            }
            else
            {
                FrmExceptionNotifier frm = new FrmExceptionNotifier(ex.Message, ex.StackTrace.Substring(3));
                frm.ShowDialog();
                frm.Focus();
                frm.btnClose.Focus();
            }
        }

        public static void Log(Exception ex)
        {
            FrmExceptionNotifier frm = new FrmExceptionNotifier(ex.Message, ex.StackTrace.Substring(3));
            frm.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
